package com.zrrd.learn.framework.mvc;

import com.zrrd.learn.framework.mvc.config.App;
import org.springframework.stereotype.Component;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

/**
 * tomcat实例化的第一配置文件
 *
 * spring boot : 内置了tomcat 8080
 *
 * web系统 ： ......
 * web服务器 : tomcat jetty  weblobic Web application server jboss
 *
 * 1 : 首先要有一个服务器  tomcat localhost:8080
 * 2 : idea里面配置tomcat
 * 3 ： 把你写好的web系部署到tomcat
 * 4 ： 启动tomcat 运行  browser
 * 5 : http://localhost:8080/app/hello
 *
 * @author wrn
 * @version 1.0
 * @date 2023/9/18 10:21
 */
@Component
public class MyWebDemo implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) {

        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(App.class);

        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/app/*");
    }

}
